/*
 * @Description: 
 * @Author: blackcai
 * @Date: 2022-05-12 13:40:47
 * @LastEditTime: 2024-11-02 16:31:54
 * @LastEditors: wuxuanfeng
 */
import axios from 'axios'
import dialog from './dialog'
const SUCCESS_CODE = 0

// 创建服务
const request = axios.create({
	baseURL: '',
	timeout: 50000
})

// 全局loading
const loading = {
  count: 0,
  loading: null,
  open () {
    this.count++
    if (this.count <= 1 && !this.loading) {
      this.loading = ElLoading.service({
				text: '加载中…'
			})
    }
  },
  close () {
		const timer = setTimeout(() => {
			this.count--
			if (this.count <= 0) {
				this.clear()
			}
      clearTimeout(timer)
		}, 100)
  },
  clear () {
    if (this.loading) {
			this.loading.close()
      this.count = 0
      this.loading = null
    }
  }
}

// 请求拦截
request.interceptors.request.use(
	config => {
    if (config.showLoading) {
      loading.open()
    }

		const token = localStorage.getItem('Access-Token')
		if (token) {
			config.headers['Authorization'] = `Bearer ${token.replace(/"/g, '')}`
		}
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

// 响应拦截
request.interceptors.response.use(
	response => {
		if (response.config.showLoading) {
			loading.close()
		}
    const { code, message } = response.data
    if (code && code !== SUCCESS_CODE) {
      dialog.error(message)
      return Promise.reject(new Error(message))
    }
    return Promise.resolve(response.data)
	},
	error => {
		loading.clear()
    const { response } = error
    if (response) {
      const { status } = response
      const msg = handleErrorMsg(status)
      dialog.error(msg)
      return Promise.reject(new Error(msg))
    } else {
      dialog.error('系统繁忙，请稍后重试!')
      return Promise.reject(error)
    }
	}
)

// 异常status处理
const handleErrorMsg = (status) => {
  let msg = ''
  switch (status) {
    case 400:
      msg = '请求错误(400)'
      break
    case 401:
      msg = '未授权，请重新登录(401)'
      break
    case 403:
      msg = '拒绝访问(403)'
      break
    case 404:
      msg = '请求错误，未找到该资源(404)'
      break
    case 408:
      msg = '请求超时(408)'
      break
    case 500:
      msg = '服务器错误(500)'
      break
    case 501:
      msg = '服务未实现(501)'
      break
    case 502:
      msg = '网络错误(502)'
      break
    case 503:
      msg = '服务不可用(503)'
      break
    case 504:
      msg = '网络超时(504)'
      break
    case 505:
      msg = 'http版本不支持该请求(505)'
      break
    default:
      msg = '系统繁忙，请稍后重试'
  }
  return msg
}

/**
 * get方法
 * @param {String} url 请求的url地址
 * @param {Object} data 请求时携带的参数
 * @param {Object} config 额外请求配置参数，便于拦截器里使用
 */
const get = (url, data = {}, options = {}) => {
	return new Promise((resolve, reject) => {
		request
			.get(url, { params: data, ...options })
			.then(response => {
				resolve(response)
			})
			.catch(err => {
				reject(err)
			})
	})
}

/**
 * post方法
 * @param {String} url 请求的url地址
 * @param {Object} data 请求时携带的参数
 * @param {Object} config 额外请求配置参数，便于拦截器里使用
 */
const post = (url, data = {}, options = {}) => {
	return new Promise((resolve, reject) => {
		request
      .post(url, data, options)
			.then(response => {
				resolve(response)
			})
			.catch(err => {
				reject(err)
			})
	})
}

export default {
	get,
	post
}